package com.ruoyi.service.impl;

import java.util.Collections;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.result.Result;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.domain.T03;
import com.ruoyi.domain.T06;
import com.ruoyi.domain.T18;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.service.IT03Service;
import com.ruoyi.service.IT06Service;
import com.ruoyi.service.IT18Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mapper.T26Mapper;
import com.ruoyi.domain.T26;
import com.ruoyi.service.IT26Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;

/**
 * token申请Service业务层处理
 *
 * @author ruoyi
 * @date 2025-04-22
 */
@Service
public class T26ServiceImpl implements IT26Service
{
    @Autowired
    private T26Mapper t26Mapper;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private IT06Service t06Service;

    @Autowired
    private IT18Service t18Service;

    @Autowired
    private IT03Service t03Service;
    /**
     * 查询token申请
     *
     * @param F2601 token申请主键
     * @return token申请
     */
    @Override
    public T26 selectT26ByF2601(Long F2601)
    {
        return t26Mapper.selectT26ByF2601(F2601);
    }

    /**
     * 查询token申请列表
     *
     * @param t26 token申请
     * @return token申请
     */
    @Override
    public List<T26> selectT26List(T26 t26)
    {
        return t26Mapper.selectT26List(t26);
    }

    /**
     * 新增token申请
     *
     * @param t26 token申请
     * @return 结果
     */
    @Override
    public int insertT26(T26 t26)
    {
        return t26Mapper.insertT26(t26);
    }

    /**
     * 修改token申请
     *
     * @param t26 token申请
     * @return 结果
     */
    @Override
    public int updateT26(T26 t26)
    {
        return t26Mapper.updateT26(t26);
    }

    /**
     * 批量删除token申请
     *
     * @param F2601s 需要删除的token申请主键
     * @return 结果
     */
    @Override
    public int deleteT26ByF2601s(Long[] F2601s)
    {
        return t26Mapper.deleteT26ByF2601s(F2601s);
    }

    /**
     * 删除token申请信息
     *
     * @param F2601 token申请主键
     * @return 结果
     */
    @Override
    public int deleteT26ByF2601(Long F2601)
    {
        return t26Mapper.deleteT26ByF2601(F2601);
    }

    @Override
    public Result addApply(T26 t26, HttpServletRequest request) {
        LoginUser loginUser = tokenService.getLoginUser(request); // 确保tokenService和getLoginUser方法存在
        Long userId = loginUser.getUserId();
        T06 t06 = t06Service.selectT06ByF0601(userId);
        t26.setF2604(1L);
        t26.setF2605(new Date());
        t26.setF0301(t06.getF0301());
        t26.setF0501(t06.getF0501());
        t26.setF0601(userId);
        t26.setF2607(t06.getF0602());
        t26.setF2608(t06.getF0618());
        t26.setF2609(t06.getF0617());
        int i = t26Mapper.insertT26(t26);
        return Result.success(i,"新增成功");
    }

    @Override
    public List<T26> selectComList(T26 t26, HttpServletRequest request) {
        LoginUser loginUser = tokenService.getLoginUser(request);
        Long userId = loginUser.getUserId();
        T18 t18 = t18Service.selectT18ByF1801(userId);
        Long F0301 = t18.getF0301();
        t26.setF0301(F0301);
        PageUtils.startPage();
        return t26Mapper.selectT26List(t26);
    }

    @Override
    @Transactional
    public Result toSuccess(T26 t26, HttpServletRequest request) {
        LoginUser loginUser = tokenService.getLoginUser(request);
        Long userId = loginUser.getUserId();
        T18 t18 = t18Service.selectT18ByF1801(userId);
        t26.setF2604(0L);
        t26.setF2606(new Date());
        t26.setF1801(t18.getF1801());
        t26.setF2610(t18.getF1804());

        Long F0301 = t18.getF0301();
        T06 t06 = t06Service.selectT06ByF0601(t26.getF0601());
        T03 t03 = t03Service.selectT03ByF0301(F0301);
        if(t03.getF0310()<t26.getF2603()){
            return Result.erro("申请数量大于公司剩余数量");
        }else{
            t03.setF0310(t03.getF0310()-t26.getF2603());
            t06.setF0611(t06.getF0611()+t26.getF2603());
        }
        int i = t26Mapper.updateT26(t26);
        t03Service.updateT03(t03);
        t06Service.updateT06(t06);
        return Result.success(i,"审核成功");
    }

    @Override
    public Result toRefuse(T26 t26, HttpServletRequest request) {
        LoginUser loginUser = tokenService.getLoginUser(request);
        Long userId = loginUser.getUserId();
        T18 t18 = t18Service.selectT18ByF1801(userId);
        t26.setF2604(2L);
        t26.setF2606(new Date());
        t26.setF1801(t18.getF1801());
        t26.setF2610(t18.getF1804());
        int i = t26Mapper.updateT26(t26);
        return Result.success(i,"审核成功");
    }

    @Override
    public Result getUserList(HttpServletRequest request) {
        LoginUser loginUser = tokenService.getLoginUser(request);
        Long userId = loginUser.getUserId();
        List<T26> list = t26Mapper.selectUserList(userId);
        return Result.success(list,"查询成功");
    }
}
